package app

import (
	"errors"

	"github.com/douyu/jupiter/pkg/store/gorm"
	"github.com/douyu/jupiter/pkg/xlog"
	"github.com/go-redis/redis"
)

var (
	//这个一定要使用,不然各种源码panic,头大
	Log = xlog.DefaultLogger

	DB *gorm.DB

	Redis *redis.Client

	DefEngine = &Engine{Error: errors.New("未初始化")}
)

func New(cfg Config, fns ...func() error) (a *Engine) {

	DefEngine = newApp(cfg, fns...)

	//捕捉错误
	defer DefEngine.stack()

	//初始化数据库
	DB = gorm.StdConfig("test").WithLogger(Log).Build()
	//表跟随表结构(不会加s)
	DB.SingularTable(true)

	//初始化redis
	Redis = redis.NewClient(&redis.Options{
		Addr:     DefEngine.Cfg().Redis.Address,
		Password: DefEngine.Cfg().Redis.Password,
	})

	return DefEngine
}
